/**
 * Application Application.java
 */

package CCT220_Aula18_Exercicio02_13106;


import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JToggleButton;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Aula18_Exercicio02_13106
 * </br>Class: Application
 * </br>Last Change: 28/10/2011 12:45:15 by marcel
 * </br>Description:
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class Application implements ItemListener {
	private JToggleButton jtb1;
	private JToggleButton jtb2;
	private JToggleButton jtb3;
	private JToggleButton jtb4;


	public Application() {

		this.start();
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula18_Exercicio02_13106
	 * </br>Class: Application
	 * </br>Method: start
	 * </br>Last Change: 28/10/2011 12:46:27 by marcel
	 * </br>Description:
	 * </br>******************************</br>
	 * 
	 */
	private void start() {

		this.startComponents();
		JFrame jf = new JFrame("Marcel M. Grilo - 13106");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setSize(200, 100);
		jf.setLayout(new GridLayout(2, 2, 2, 2));
		jf.add(jtb1);
		jf.add(jtb2);
		jf.add(jtb3);
		jf.add(jtb4);
		jf.setVisible(true);
	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula18_Exercicio02_13106
	 * </br>Class: Application
	 * </br>Method: startComponents
	 * </br>Last Change: 28/10/2011 12:49:42 by marcel
	 * </br>Description:
	 * </br>******************************</br>
	 * 
	 */
	private void startComponents() {

		jtb1 = new JToggleButton("Botao 1");
		jtb1.addItemListener(this);
		jtb2 = new JToggleButton("Botao 2");
		jtb2.addItemListener(this);
		jtb3 = new JToggleButton("Botao 3");
		jtb3.addItemListener(this);
		jtb4 = new JToggleButton("Botao 4");
		jtb4.addItemListener(this);
		selectAllTo(true);

	}


	public static void main(String[] args) {

		new Application();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
	 */
	@Override
	public void itemStateChanged(ItemEvent e) {

		if (e.getSource() == jtb1) {

			if (jtb1.isSelected()) {
				jtb1.removeItemListener(this);
				selectAllTo(true);
				jtb1.setSelected(true);
				jtb1.addItemListener(this);
			} else {
				jtb1.removeItemListener(this);
				selectAllTo(true);
				jtb1.setSelected(false);
				jtb1.addItemListener(this);
			}
		} else if (e.getSource() == jtb2) {

			if (jtb2.isSelected()) {
				jtb2.removeItemListener(this);
				selectAllTo(true);
				jtb2.setSelected(true);
				jtb2.addItemListener(this);
			} else {
				jtb2.removeItemListener(this);
				selectAllTo(true);
				jtb2.setSelected(false);
				jtb2.addItemListener(this);
			}
		} else if (e.getSource() == jtb3) {

			if (jtb3.isSelected()) {
				jtb3.removeItemListener(this);
				selectAllTo(true);
				jtb3.setSelected(true);
				jtb3.addItemListener(this);
			} else {
				jtb3.removeItemListener(this);
				selectAllTo(true);
				jtb3.setSelected(false);
				jtb3.addItemListener(this);
			}
		} else if (e.getSource() == jtb4) {

			if (jtb4.isSelected()) {
				jtb4.removeItemListener(this);
				selectAllTo(true);
				jtb4.setSelected(true);
				jtb4.addItemListener(this);
			} else {
				jtb4.removeItemListener(this);
				selectAllTo(true);
				jtb4.setSelected(false);
				jtb4.addItemListener(this);
			}
		}

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula18_Exercicio02_13106
	 * </br>Class: Application
	 * </br>Method: selectAllTo
	 * </br>Last Change: 28/10/2011 12:58:38 by marcel
	 * </br>Description:
	 * </br>******************************</br>
	 * 
	 * @param b
	 */
	private void selectAllTo(boolean b) {

		jtb1.setSelected(b);
		jtb2.setSelected(b);
		jtb3.setSelected(b);
		jtb4.setSelected(b);
	}

}
